﻿using DAO.db;
using Entity.shangping;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace DAO.shangping
{
    /// <summary>
    /// 商品分类数据库操作类
    /// </summary>
    public  class GoodsTypeDao
    {
        /// <summary>
        /// 保存方法
        /// </summary>
        /// <param name="goodsType">商品分类对象</param>
        /// <returns>掉用保存方法的返回结果，如果成功返回字符串（保存成功），否则返回字符串（保存失败）</returns>
        public string Save(GoodsType goodsType)
        {
            string ret = "";

            if (string.IsNullOrEmpty(goodsType.ID))//如果没有ID，则表示添加
            {
                goodsType.ID = Guid.NewGuid().ToString();
                using (EFDbContext dbContext = new EFDbContext())
                {
                    dbContext.GoodsTypes.Add(goodsType);
                    dbContext.SaveChanges();
                    ret= "保存成功";
                }
            }
            else//否则表示修改
            {
                using (EFDbContext dbContext = new EFDbContext())
                {
                    dbContext.Entry(goodsType).State = EntityState.Modified;
                    dbContext.SaveChanges();
                    ret= "保存成功";
                }
            }

            return ret;
        }

        /// <summary>
        /// 根据ID查询某个商品分类对象
        /// </summary>
        /// <param name="ID">商品分类ID</param>
        /// <returns>商品分类对象（表里的一行数据）</returns>
        public GoodsType GetByID(string ID)
        {
            GoodsType g = new GoodsType();

            using (EFDbContext dbContext = new EFDbContext())
            {
                g = dbContext.GoodsTypes.Find(ID);
            }
            
            return g;
        }

        /// <summary>
        /// 查询商品分类列表
        /// </summary>
        /// <param name="name">分类名称</param>
        /// <returns>商品分类集合对象</returns>
        public List<GoodsType> GetList(string name)
        {
            List<GoodsType> ls = new List<GoodsType>();
            using (EFDbContext dbContext = new EFDbContext())
            {
                var query = dbContext.GoodsTypes.AsQueryable();

                if (!string.IsNullOrEmpty(name))
                {
                    query.Where(t => t.Name.Contains(name));
                }
                ls = query.ToList();
            }
            return ls;
        }

        /// <summary>
        /// 停用分类
        /// </summary>
        /// <param name="ID">分类ID</param>
        /// <returns>1表示停用成功，否则停用失败</returns>
        public int Disable(string ID)
        {
            int ret = 0;
            using (EFDbContext dbContext = new EFDbContext())
            {
                var g = dbContext.GoodsTypes.Find(ID);
                if (g != null)
                {
                    g.Status = 0;
                    dbContext.Entry(g).State = EntityState.Modified;
                    dbContext.SaveChanges();
                    ret = 1;
                }
            }
            return ret;
        }


        /// <summary>
        /// 启用分类
        /// </summary>
        /// <param name="ID">分类ID</param>
        /// <returns>1表示启用成功，否则启用失败</returns>
        public int Enable(string ID)
        {
            int ret = 0;
            using (EFDbContext dbContext = new EFDbContext())
            {
                var g = dbContext.GoodsTypes.Find(ID);
                if (g != null)
                {
                    g.Status = 1;
                    dbContext.Entry(g).State = EntityState.Modified;
                    dbContext.SaveChanges();
                    ret = 1;
                }
            }
            return ret;
        }


        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="ID">分类ID</param>
        /// <returns>1表示删除成功，否则删除失败</returns>
        public int Delete(string ID)
        {
            int ret = 0;
            using (EFDbContext dbContext = new EFDbContext())
            {
                var g = dbContext.GoodsTypes.Find(ID);
                if (g != null)
                {
                    ////删除方式一
                    //dbContext.GoodsTypes.Attach(user);
                    //dbContext.GoodsTypes.Remove(user);
                    //删除方式二
                    dbContext.Entry(g).State = EntityState.Deleted;
                    dbContext.SaveChanges();
                    ret = 1;
                }
               
            }
            return ret;
        }


    }
}
